/*
 * Copyright (C) 2014 Alpha, Inc.
 * This is NOT a freeware, use is subject to license terms.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.gehua.octopus.rpc.utils;

import com.google.common.base.Charsets;
import java.lang.reflect.Method;
import java.nio.charset.Charset;

/**
 *
 * @author guna
 */
public class StringUtil {

    private StringUtil() {

    }

    public static String replace(String s, String sub, String with) {
        int c = 0;
        int i = s.indexOf(sub, c);
        if (i == -1) {
            return s;
        }

        StringBuilder buf = new StringBuilder(s.length() + with.length());

        do {
            buf.append(s.substring(c, i));
            buf.append(with);
            c = i + sub.length();
        } while ((i = s.indexOf(sub, c)) != -1);

        if (c < s.length()) {
            buf.append(s.substring(c, s.length()));
        }

        return buf.toString();

    }

    public static void append(StringBuilder buf,
            String s,
            int offset,
            int length) {
        synchronized (buf) {
            int end = offset + length;
            for (int i = offset; i < end; i++) {
                if (i >= s.length()) {
                    break;
                }
                buf.append(s.charAt(i));
            }
        }
    }

    /* ------------------------------------------------------------ */
    /**
     * append hex digit
     *
     * @param buf
     * @param b
     * @param base
     */
    public static void append(StringBuilder buf, byte b, int base) {
        int bi = 0xff & b;
        int c = '0' + (bi / base) % base;
        if (c > '9') {
            c = 'a' + (c - '0' - 10);
        }
        buf.append((char) c);
        c = '0' + bi % base;
        if (c > '9') {
            c = 'a' + (c - '0' - 10);
        }
        buf.append((char) c);
    }

    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    public static void append2digits(StringBuilder buf, int i) {
        if (i < 100) {
            buf.append((char) (i / 10 + '0'));
            buf.append((char) (i % 10 + '0'));
        }
    }

    /* ------------------------------------------------------------ */
    /**
     * Return a non null string.
     *
     * @param s String
     * @return The string passed in or empty string if it is null.
     */
    public static String nonNull(String s) {
        if (s == null) {
            return "";
        }
        return s;
    }

    public static boolean isSystemUTF8() {
        Charset defaultCharset = java.nio.charset.Charset.defaultCharset();
        return Charsets.UTF_8.equals(defaultCharset);
    }

    public static String getWebsocketActionName(Method method) {
        if (method == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder(150);
        String controllerName = method.getDeclaringClass().getSimpleName();
        String actionName = method.getName();
        if (controllerName.toLowerCase().endsWith("controller")) {
            controllerName = controllerName.substring(0, controllerName.length() - 10);
        }
        sb.append(controllerName).append(".").append(actionName);
        return sb.toString();
    }

    public static String getHTTPActionName(Method method) {
        if (method == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder(150);
        String controllerName = method.getDeclaringClass().getSimpleName();
        String actionName = method.getName();
        if (controllerName.toLowerCase().endsWith("controller")) {
            controllerName = controllerName.substring(0, controllerName.length() - 10);
        }
        sb.append(controllerName).append("#").append(actionName);
        return sb.toString();
    }

    public static String getActionName(Method method) {
        if (method == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder(150);
        sb.append(method.getDeclaringClass().getName()).append('.')
                .append(method.getName());
        for (Class input : method.getParameterTypes()) {
            sb.append('#').append(input.getName());
        }
        Class<?> returnType = method.getReturnType();
        sb.append("*").append(returnType == null ? "void" : returnType.getName());
        return sb.toString();

    }

}
